package com.cff.ldap;

import java.io.UnsupportedEncodingException;

import org.junit.Test;

import com.cff.util.LdapUtil;
import com.novell.ldap.LDAPAttribute;
import com.novell.ldap.LDAPAttributeSet;
import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPEntry;
import com.novell.ldap.LDAPException;

public class TestLdap {
	
	//@Test
	public void testSearch(){
		LDAPConnection lc;
		LDAPEntry le = null;
		try {
			lc = LdapUtil.connection();
			le = lc.read("dc=visit,dc=chinaums");
		} catch (UnsupportedEncodingException e) {
			System.out.println("不支持的编码");
		} catch (LDAPException e) {
			System.out.println("查找失败"+e.getMessage());
		}
		System.out.println(le.getAttributeSet());
	}
	
	@Test
	public void testAdd(){
		LDAPConnection lc;
		LDAPEntry le = null;
		String dn= "cn=13918255259,dc=visit,dc=chinaums";
		LDAPAttributeSet attributeSet = new LDAPAttributeSet();
		
		attributeSet.add(new LDAPAttribute("objectclass", new String(
				"person")));
		attributeSet.add(new LDAPAttribute("cn", "13918255259"));
		attributeSet.add(new LDAPAttribute("sn", "jun"));
		//attributeSet.add(new LDAPAttribute("Phone","15607110725"));
		attributeSet.add(new LDAPAttribute("userPassword", "889988"));
		
		try {
			lc = LdapUtil.connection();
			//le = lc.read("cn=Manager,dc=my-domain,dc=com");
			LDAPEntry newEntry = new LDAPEntry( dn, attributeSet);
			lc.add(newEntry);
		} catch (UnsupportedEncodingException e) {
			System.out.println("不支持的编码");
		} catch (LDAPException e) {
			System.out.println("添加失败"+e.getMessage());
			e.printStackTrace();
		}
		searchDn(dn);
	}
	
	//@Test
	public void testAddBase(){
		LDAPConnection lc;
		LDAPEntry le = null;
		String dn= "dc=visit,dc=chinaums";
		LDAPAttributeSet attributeSet = new LDAPAttributeSet();
		
		attributeSet.add(new LDAPAttribute("objectclass", new String(
				"domain")));
		attributeSet.add(new LDAPAttribute("objectclass", new String(
				"top")));
		attributeSet.add(new LDAPAttribute("o", new String(
				"visit")));
		attributeSet.add(new LDAPAttribute("dc", new String(
				"visit")));
		
		try {
			lc = LdapUtil.connection();
			//le = lc.read("cn=Manager,dc=my-domain,dc=com");
			LDAPEntry newEntry = new LDAPEntry( dn, attributeSet);
			lc.add(newEntry);
		} catch (UnsupportedEncodingException e) {
			System.out.println("不支持的编码");
		} catch (LDAPException e) {
			System.out.println("添加失败"+e.getMessage());
			e.printStackTrace();
		}
		searchDn(dn);
	}
	
	//@Test
	public void testRemove(){
		LDAPConnection lc;
		LDAPEntry le = null;
		String dn= "cn=15607110725,dc=my-domain,dc=com";
		
		try {
			lc = LdapUtil.connection();
			lc.delete(dn);
		} catch (UnsupportedEncodingException e) {
			System.out.println("不支持的编码");
		} catch (LDAPException e) {
			System.out.println("删除失败"+e.getMessage());
			e.printStackTrace();
		}
		searchDn(dn);
	}
	
	public static void searchDn(String dn){
		LDAPConnection lc;
		LDAPEntry le = null;
		try {
			lc = LdapUtil.connection();
			le = lc.read(dn);
		} catch (UnsupportedEncodingException e) {
			System.out.println("不支持的编码");
		} catch (LDAPException e) {
			System.out.println("查找失败"+e.getMessage());
		}
		System.out.println(le.getAttributeSet());
	}
}	
